/***************************************************
This file takes all the graduates in the all_students file,
which have been flagged, and extracts when they were enrolled 
***************************************************/

data enrollment (keep = pik degcip_2dig opeid  credithours reportterm 
				reportyear year quarter state cte_flag deglevl_code )
    /view=enrollment;
    
    merge INPUTS.pseo_ny_cuny_2001_2020_enr_rsch (in=a) 
          INPUTS.pseo_ny_cuny_2001_2020_enr_qpik (in=b rename=(qpik_ssn = pik)) ;
    by dqb_source_id ; 

	state = '36' ; 
    deglevl_code = degpur ; 
    degcip_2dig = substr(degcip,1,2) ;
    year = input(yearenr,4.)  ; 
    if semenr = '02' then quarter = 1 ; 
	if semenr = '06' then quarter = 2 ; 
	if semenr = '09' then quarter = 3 ; 
	if semenr = '12' then quarter = 4 ; 
    
    if year>=2001 & deglevl_code in ('01','02','03','04') ;
    credithours = input(crdhrs,4.) ;
run;

proc sort data=enrollment out=enrollmentsort; 
    by pik opeid ; 
run;
    
proc sort data=OUTPUTS.all_students_cuny out=all ; 
    by pik opeid ; 
run; 

data OUTPUTS.enrollment_long_cuny; 
    merge enrollmentsort (in=a) all (in=b drop=year quarter) ;
    by pik opeid ; 
    
    if b ; 
    if year = . then year=year_grad ;
    if quarter = . then quarter = quarter_grad ; 
    enroll= a ; 
    if pik ne '' ; 
    qtime = %qtime(year,quarter,location=datastep) ;
run;

proc sort data=OUTPUTS.enrollment_long_cuny ;
	by pik opeid deglevl_code degcip_2dig qtime ; 
run;

proc sort data=OUTPUTS.enrollment_long_cuny out=sample_flag ; 
    by pik opeid ; 
run;


/****************************
Making enrollment wide file
*****************************/

data OUTPUTS.enrollment_wide_cuny (keep = pik opeid degcip_2dig credithours 
										flag_cte graduate enr61-enr138 state deglevl_code
										qtime_grad)  ;
	set sample_flag ;
	by pik opeid ;
	
	retain enr21-enr138 ;
	array enr{21:138} enr21-enr138;  
	if qtime <= 138 ; 
	if first.opeid then do ; 
        do i = 21 to 138 ; 
            enr{i} = 0 ; 
        end;
    end;
         
    enr{qtime} = enroll ;  
            
    if last.pik then output ; 
run;

proc sort data=OUTPUTS.enrollment_wide_cuny ;
	by pik opeid deglevl_code degcip_2dig ; 
run;
